<template>
  <div class="container">
    <el-card class="box-card">
      <el-form :inline="true" :model="formInline" class="mulu">
        <div>
          <el-form-item label="目录名称" class="muluName">
            <el-input v-model="formInline.user"></el-input>
          </el-form-item>
          <el-form-item label="状态" class="status">
            <el-select v-model.trim="formInline.region" placeholder="请选择">
              <el-option label="启动" value="1"></el-option>
              <el-option label="禁止" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button class="delete" @click="deleteData">清除</el-button>
            <el-button type="primary" class="search" @click="searchData"
              >搜索</el-button
            >
          </el-form-item>
        </div>

        <el-button
          type="success"
          icon="el-icon-edit"
          class="addList"
          @click="AddValue"
          >新增目录</el-button
        >
      </el-form>
      <el-alert
        :title="`数据一共${page.counts}条`"
        type="info"
        :closable="false"
        show-icon
        class="point out"
      >
      </el-alert>
      <template>
        <el-table
          :data="DirectorysList"
          style="width: 100%"
          class="table"
          :header-cell-style="{ background: '#eef1f6', color: '#606266' }"
        >
          <el-table-column type="index" label="序号" width="80">
          </el-table-column>
          <el-table-column prop="subjectName" label="所属学科" width="280">
          </el-table-column>
          <el-table-column prop="directoryName" label="目录名称">
          </el-table-column>
          <el-table-column prop="username" label="创建者"> </el-table-column>
          <el-table-column prop="addDate" label="创建日期"> </el-table-column>
          <el-table-column prop="totals" label="面试题数量"> </el-table-column>
          <el-table-column prop="state" label="状态">
            <template #default="{ row }">
              {{ row.state === 1 ? "已启用" : "已禁用" }}
            </template>
          </el-table-column>
          <el-table-column prop="operate" label="操作">
            <template #default="{ row }">
              <el-button type="text" @click="stateOn(row)">
                <span>{{ row.state === 0 ? "启用" : "禁用" }}</span>
              </el-button>
              <el-button
                type="text"
                @click="changeBtn(row)"
                :disabled="row.state === 0"
              >
                修改</el-button
              >
              <el-button
                type="text"
                @click="DeleteValue(row.id)"
                :disabled="row.state === 0"
              >
                删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </template>
      <el-row type="flex" justify="end" align="end" style="height: 60px">
        <el-pagination
          background
          layout="prev, pager, next,sizes,jumper"
          :current-page.sync="page.page"
          :page-sizes="pageNumber"
          :page-size="page.pagesize"
          :total="page.counts"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </el-card>
    <DirectorysAdd
      :showDialog.sync="showDialog"
      ref="updateAdd"
      @getList="getList"
      :subjectsID="subjectID"
    ></DirectorysAdd>
  </div>
</template>

<script>
import { list, changeState, remove } from "@/api/hmmm/directorys";
import DirectorysAdd from "../components/directorys-add.vue";
import { detail } from "@/api/hmmm/subjects";
export default {
  name: "DirectorysPage",
  components: {
    DirectorysAdd,
  },
  data() {
    return {
      formInline: {
        user: "",
        region: "",
      },
      pageNumber: [5, 10, 20, 40, 60, 80, 100],
      page: {
        page: 1, // 当前页码
        pagesize: 5, // 每页多少条
        counts: 0,
        directoryName: null, // 目录名称
        state: null, // 目录状态
        id: null, // 学科的id
        subjectID: null, // 目录列表需要的参数
      },
      DirectorysList: [],
      showDialog: false,
      subjectID: this.$route.params.id,
      subjects: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async getList() {
      // 数据
      if (this.formInline.region.length) {
        this.page.state = this.formInline.region - 0;
      }
      if (this.subjectID) {
        this.page.subjectID = this.subjectID;
        this.page.id = this.subjectID;
        const { data } = await detail({ id: this.page.id });
        this.subjects = data;
        console.log(this.subjects);
      }
      this.page.directoryName = this.formInline.user;
      const { data } = await list(this.page);
      this.DirectorysList = data.items;
      this.page.counts = data.counts;
      console.log(this.DirectorysList);
    },
    async stateOn(row) {
      // this.stateVal = "禁用";
      // const state = await changeState(id, state);
      if (row.state === 1) {
        row.state = 0;
      } else {
        row.state = 1;
      }
      // console.log(state);
      await changeState(row);
      this.$message.success("操作成功");
    },
    async DeleteValue(id) {
      // 删除
      try {
        await this.$confirm("确定删除吗");
        await remove(id);
        this.$message.success("删除成功");
        this.getList();
      } catch (error) {
        console.log(error);
      }
    },
    async AddValue() {
      // 显示弹层
      this.showDialog = true;
    },
    async changeBtn(row) {
      // 调子组件里的updateDirectorys方法
      await this.$refs.updateAdd.updateDirectorys(row, this.subjectID);
      // 显示弹层
      this.showDialog = true;
    },
    deleteData() {
      // 清空数据
      this.formInline = {
        user: "",
        region: "",
      };
      this.page.page = 1;
      this.getList();
    },
    async searchData() {
      // 查询
      // this.DirectoryList = [];
      // const {
      //   data: { items },
      // } = await this.getList();
      this.page.page = 1;
      // this.directoryList = items;
      // this.total = this.data.counts;
      this.getList();
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.page.pagesize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.page.page = val;
      this.getList();
    },
  },
};
</script>

<style scoped lang='scss'>
.box-card {
  margin-left: 10px;
  margin-right: 10px;
  margin-top: 10px;
}
.mulu {
  width: 100%;
  display: flex;
  justify-content: space-between;
}
.mulu .status {
  margin-left: 30px;
}
.mulu .addList {
  margin-right: 13px;
  height: 40px;
}

.mulu .delete {
  margin-left: 10px;
}
.table {
  margin-bottom: 20px;
}
</style>
